home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1995, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* mask.c
- * This program draws a rotating nurbs surface shaped like a mask.
- *
- * Escape key - exit the program
- * <t> key - toggle texture mapping on/off
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
-
- #include "rgbImageFile.h" /* should be in ../../include */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid initSurface( GLvoid );
- GLvoid initTexture( unsigned int *image,
- GLsizei imageWidth, GLsizei imageHeight );
- GLvoid toggleTexture( GLvoid );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLboolean textureFlag = GL_FALSE;
-
- static GLfloat ctlpoints[10][7][3] =
- { /* t = 0, t = 1, t = 2, t = 3, t = 4, t = 5, t = 6 */
- { {-1.4,7.1,0.0},{-1.2,7.1,0.2},{-1.0,7.1,0.3},{0.0,7.1,0.5},{1.0,7.1,0.3},{1.2,7.1,0.2},{1.4,7.1,0.0} }, /* s=0 */
- { {-1.6,7.0,0.0},{-1.4,7.0,0.7},{-1.0,7.0,0.9},{0.0,7.0,1.1},{1.0,7.0,0.9},{1.4,7.0,0.7},{1.6,7.0,0.0} }, /* s=1 */
- { {-1.8,4.5,0.0},{-1.5,4.5,0.5},{-1.0,4.5,0.7},{0.0,4.5,0.9},{1.0,4.5,0.7},{1.5,4.5,0.5},{1.8,4.5,0.0} }, /* s=2 */
- { {-1.9,2.6,0.0},{-1.5,2.6,1.1},{-1.0,2.6,1.0},{0.0,2.6,2.5},{1.0,2.6,1.0},{1.5,2.6,1.1},{1.9,2.6,0.0} }, /* s=3 */
- { {-1.8,2.8,0.0},{-1.5,2.8,0.8},{-1.0,2.8,0.9},{0.0,2.8,1.0},{1.0,2.8,0.9},{1.5,2.8,0.8},{1.8,2.8,0.0} }, /* s=4 */
- { {-1.8,1.8,0.0},{-1.5,1.8,0.8},{-1.0,1.8,1.0},{0.0,1.8,1.2},{1.0,1.8,1.0},{1.5,1.8,0.8},{1.8,1.8,0.0} }, /* s=5 */
- { {-1.8,1.6,0.0},{-1.5,1.6,0.7},{-1.0,1.6,0.8},{0.0,1.6,0.9},{1.0,1.6,0.8},{1.5,1.6,0.7},{1.8,1.6,0.0} }, /* s=6 */
- { {-1.7,0.9,0.0},{-1.5,0.9,0.8},{-1.0,0.9,1.0},{0.0,0.9,1.1},{1.0,0.9,1.0},{1.5,0.9,0.8},{1.7,0.9,0.0} }, /* s=7 */
- { {-1.5,.12,0.0},{-1.2,.12,0.6},{-1.0,.12,0.7},{0.0,.12,0.8},{1.0,.12,0.7},{1.2,.12,0.6},{1.5,.12,0.0} }, /* s=8 */
- { {-0.8,.15,0.0},{-1.1,.15,0.0},{-1.0,.15,0.0},{0.0,.15,0.0},{1.0,.15,0.0},{1.1,.15,0.0},{0.8,.15,0.0} } /* s=9 */
- };
-
- static GLUnurbsObj *theNurb;
-
- static unsigned int *image;
- static GLsizei imageWidth, imageHeight;
-
- static GLfloat angle = 0.0;
-
- void
- main(int argc, char *argv[])
- {
- GLsizei width, height;
- char *imageFileName = "cv.rgb";
-
- fprintf(stdout, "loading image %s\n", imageFileName );
- image = rgbReadImageFile(imageFileName, &imageWidth,
- &imageHeight);
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width/4, height/4 );
- glutInitWindowSize( width/2, height/2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - draw a rotating mask made of "
- "a NURBS surface with trimming curves.\n\n"
- "Escape key - exit the program\n"
- "<t> key - toggle texture mapping on/off\n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- GLfloat mat_diffuse[] = { 1.0, 0.6, 0.0, 1.0 };
- GLfloat mat_emission[] = { 0.0, 0.0, 0.0, 1.0 };
-
- GLfloat light_position[] = { 1.0, 0.0, 1.0, 0.0 };
-
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_diffuse);
- glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
-
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
-
- glClearColor( 0, 0, 0.8, 1 );
- glEnable( GL_DEPTH_TEST );
-
- /* enable automatic surface normal generation */
- glEnable( GL_AUTO_NORMAL );
-
- theNurb = gluNewNurbsRenderer();
-
- initTexture( image, imageWidth, imageHeight );
- }
-
- GLvoid initTexture( unsigned int *image,
- GLsizei imageWidth, GLsizei imageHeight )
- {
- /* scale texture image, make mipmaps and load texture
- * gluBuild2DMipmaps( target, components, width, height,
- * format, type, imageArray )
- */
- gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imageWidth, imageHeight,
- GL_RGBA, GL_UNSIGNED_BYTE, image);
-
-
- /* Decal uses only the texture color, not the original polygon
- * color, unless there is alpha in texture, in which case
- * alpha determines the percentage of texture blended
- * with the polygon color.
- *
- * Decal may be faster than the default modulate mode.
- */
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
-
- /* Set texture coordinate generation function to use
- * spherical coordinate mapping
- */
- glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
- glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
- }
-
- GLvoid toggleTexture( GLvoid )
- {
- textureFlag = !textureFlag;
- if (textureFlag)
- {
- glEnable(GL_TEXTURE_GEN_S);
- glEnable(GL_TEXTURE_GEN_T);
- glEnable(GL_TEXTURE_2D);
- }
- else
- {
- glDisable(GL_TEXTURE_GEN_S);
- glDisable(GL_TEXTURE_GEN_T);
- glDisable(GL_TEXTURE_2D);
- }
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 't': /* toggle texture mapping on/off */
- toggleTexture();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- animate( GLvoid )
- {
- /* update the current angle */
- angle = fmodf( (angle + 5.0), 360.0 );
-
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, 3.0, 25.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.0, 0.0, -20.0 );
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
- GLfloat edgePt[5][2] = /* counter clockwise */
- {{0.0, 0.0}, {7.0, 0.0}, {7.0, 8.0}, {0.0, 8.0}, {0.0, 0.0}};
-
- GLfloat curveKnots[] =
- {0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 2.0, 2.0, 2.0};
- GLfloat leftEyeCurve[5][2] = /* clockwise */
- {{1.3, 3.0}, {1.3, 1.0}, {1.1, 1.0}, {1.1, 3.0}, {1.3, 3.0}};
- GLfloat rightEyeCurve[5][2] = /* clockwise */
- {{1.3, 5.0}, {1.1, 5.0}, {1.1, 7.0}, {1.3, 7.0}, {1.3, 5.0}};
- GLfloat s_knots[14] =
- {
- 0.0,0.0,0.0,0.0,
- 1.0,2.0,3.0,4.0,5.0,6.0,
- 7.0,7.0,7.0,7.0
- };
-
- GLfloat t_knots[11] = { 0.0,0.0,0.0,0.0,2.0,4.0,6.0,8.0,8.0,8.0,8.0 };
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glPushMatrix();
- glTranslatef( 0.0, -4.0, 0.0 );
- glRotatef( angle, 0.0, 1.0, 0.0 );
- gluBeginSurface(theNurb);
- gluNurbsSurface(theNurb,
- 14, s_knots,
- 11, t_knots,
- 7 * 3, 3,
- &ctlpoints[0][0][0],
- 4, 4,
- GL_MAP2_VERTEX_3);
- gluBeginTrim (theNurb);
- gluPwlCurve (theNurb, 5, &edgePt[0][0], 2,
- GLU_MAP1_TRIM_2);
- gluEndTrim (theNurb);
-
- gluBeginTrim (theNurb);
- gluNurbsCurve (theNurb, 9, curveKnots, 2,
- &leftEyeCurve[0][0], 4, GLU_MAP1_TRIM_2);
- gluEndTrim (theNurb);
- gluBeginTrim (theNurb);
- gluNurbsCurve (theNurb, 9, curveKnots, 2,
- &rightEyeCurve[0][0], 4, GLU_MAP1_TRIM_2);
- gluEndTrim (theNurb);
- gluEndSurface(theNurb);
-
- glPopMatrix();
-
- glutSwapBuffers();
- }
-
-